﻿using System;
using System.Reflection;
using System.Configuration;
using Best.Framework.IDAL;
namespace Best.Framework.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口) 
    /// DataCache类在导出代码的文件夹里
    /// <appSettings> 
    /// <add key="DAL" value="DQYT.HTDZ.OracleDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];

        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }














        /// <summary>
        /// 创建BDPOVERMACHINE数据层接口。电机表抽油机上的电机
        /// </summary>
        public static DQYT.HTDZ.IDAL.IBDPOVERMACHINE CreateBDPOVERMACHINE()
        {

            string ClassNamespace = AssemblyPath + ".BDPOVERMACHINEDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.IBDPOVERMACHINE)objType;
        }


















        /// <summary>
        /// 创建部门数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.IBDDepartment CreateBDDepartment()
        {

            string ClassNamespace = AssemblyPath + ".BDDepartmentDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.IBDDepartment)objType;
        }

        /// <summary>
        /// 创建菜单数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.IBDMenu CreateBDMenu()
        {

            string ClassNamespace = AssemblyPath + ".BDMenuDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.IBDMenu)objType;
        }

        /// <summary>
        /// 创建角色数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.IBDRole CreateBDRole()
        {

            string ClassNamespace = AssemblyPath + ".BDRoleDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.IBDRole)objType;
        }

        /// <summary>
        /// 创建角色菜单数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.IBDRoleMenu CreateBDRoleMenu()
        {

            string ClassNamespace = AssemblyPath + ".BDRoleMenuDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.IBDRoleMenu)objType;
        }

        /// <summary>
        /// 创建用户数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.IBDUser CreateBDUser()
        {

            string ClassNamespace = AssemblyPath + ".BDUserDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.IBDUser)objType;
        }

        /// <summary>
        /// 创建射孔结算方法数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.ITB_SKJSDAL CreateTB_SKJS()
        {

            string ClassNamespace = AssemblyPath + ".TB_SKJSDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.ITB_SKJSDAL)objType;
        }

        /// <summary>
        /// 创建单井成本数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.ITB_DJSKCBDAL CreateTB_DJSKCB()
        {

            string ClassNamespace = AssemblyPath + ".TB_DJSKCBDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.ITB_DJSKCBDAL)objType;
        }

        /// <summary>
        /// 创建车辆路途费数据层接口
        /// </summary>
        /// <returns></returns>
        public static DQYT.HTDZ.IDAL.ITB_CLLTFDAL CreateTB_CLLTF()
        {

            string ClassNamespace = AssemblyPath + ".TB_CLLTFDAL";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (DQYT.HTDZ.IDAL.ITB_CLLTFDAL)objType;
        }
    }
}